package StreamOf;

import java.util.Arrays;
import java.util.List;

/**
 * @author zc
 * @version 1.0
 * @date 2021/9/24 15:07
 *
 * [自定义：定制排序]
 * sorted(Comparator com)：定制排序，自定义Comparator排序器
 */
public class Test10 {

    public static void main(String[] args) {
        Student s1 = new Student("aa", 10);
        Student s2 = new Student("bb", 20);
        Student s3 = new Student("aa", 30);
        Student s4 = new Student("dd", 40);
        List<Student> studentList = Arrays.asList(s1, s2, s3, s4);

//        //自定义排序：先按姓名升序，姓名相同则按年龄升序
//        studentList.stream().sorted(
//                (o1, o2) -> {
//                 if (o1.getName().equals(o2.getName())){
//                     return o1.getAge()- o2.getAge();
//                 }else{
//                     return o1.getName().compareTo(o2.getName());
//                 }
//                }
//        ).forEach(student -> System.out.println(student.getName()+"==="+student.getAge()));
//

//        //【顺着就是升序】
//                //自定义排序：先按姓名降序，姓名相同则按年龄降序
//        studentList.stream().sorted(
//                (o2, o1) -> {
//                 if (o1.getName().equals(o2.getName())){
//                     return o1.getAge()- o2.getAge();
//                 }else{
//                     //System.out.println(o1.getName().compareTo(o2.getName()));
//                     return o1.getName().compareTo(o2.getName());
//                 }
//                }
//        ).forEach(student -> System.out.println(student.getName()+"==="+student.getAge()));


        //【顺着就是升序】
        //自定义排序：先按姓名降序，姓名相同则按年龄升序
        studentList.stream().sorted(
                (o1, o2) -> {
                    if (o1.getName().equals(o2.getName())){
                        return o1.getAge()- o2.getAge();
                    }else{
                        //System.out.println(o1.getName().compareTo(o2.getName()));
                        return o2.getName().compareTo(o1.getName());
                    }
                }
        ).forEach(student -> System.out.println(student.getName()+"==="+student.getAge()));

    }
}


class Student{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }


    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}